/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

package com.huawei.exam;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @author z00611952
 * @since 2021/9/17
 */
public class Main1 {
    /**
     * main入口由OJ平台调用
     */
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
        int curTableSize = Integer.parseInt(br.readLine().trim());
        List<NeInfo> curTable = new ArrayList<>(curTableSize);
        for (int i = 0; i < curTableSize; i++) {
            String[] params = br.readLine().split(" ");
            curTable.add(new NeInfo(Integer.parseInt(params[0]), params[1], Integer.parseInt(params[2])));
        }

        int bakTableSize = Integer.parseInt(br.readLine().trim());
        List<NeInfo> bakTable = new ArrayList<>(bakTableSize);
        for (int i = 0; i < bakTableSize; i++) {
            String[] params = br.readLine().split(" ");
            bakTable.add(new NeInfo(Integer.parseInt(params[0]), params[1]));
        }
        Solution solution = new Solution();
        List<NeInfo> result = solution.syncNeInfo(curTable, bakTable);
        StringBuilder output = new StringBuilder();
        if (result != null) {
            for (NeInfo neInfo : result) {
                output.append(neInfo).append(System.lineSeparator());
            }
            System.out.print(output);
        }
    }

}

/**
 * 4
 * 256 NE256 1
 * 290 NE290 0
 * 268 26800000 1
 * 257 NE257 1
 * 3
 * 273 NE273
 * 268 NE268
 * 257 NE257
 */
class Solution {
    private List<NeInfo> reslut = new ArrayList<>();

    // 待实现函数，在此函数中填入答题代码
    List<NeInfo> syncNeInfo(List<NeInfo> curTable, List<NeInfo> bakTable) {
        Map<Integer, NeInfo> map = new HashMap<>();

        curTable.forEach((item -> {
            map.put(item.neId, item);
        }));

        bakTable.forEach(item -> {
            if (map.keySet().contains(item.neId)) {
                if (!item.neName.equals(map.get(item.neId).neName)) {
                    map.get(item.neId).neName = item.neName;
                }
            } else {
                // 不存在此key
                map.put(item.neId, item);
            }

        });
        for (Map.Entry<Integer, NeInfo> entries : map.entrySet()) {
            reslut.add(entries.getValue());
        }
        Collections.sort(reslut, Comparator.comparingInt(item -> item.neId));
        return reslut;
    }
}

class NeInfo {
    int neId;

    String neName;

    int neFlag;

    public NeInfo(int neId, String neName, int neFlag) {
        this.neId = neId;
        this.neName = neName;
        this.neFlag = neFlag;
    }

    public NeInfo(int neId, String neName) {
        this(neId, neName, 0);
    }

    @Override
    public String toString() {
        return String.format(Locale.ROOT, "%d %s %d", neId, neName, neFlag);
    }
}

